<?php

/**
 * 订阅模型
 */
class MookAction extends UserAction {

    protected $mod;

    public function _initialize() {
        parent::_initialize();
        $this->mod = M('Mook');
    }

    function index() {
        $uid = session('uid');
        $list = $this->mod->select();
        $id = (int) $this->_get("id");
        $id = (false == $id) ? $list[0]['id'] : $id;
        $mook = $this->mod->find($id);
        $feed = M('MookFeed')->where(array('mook_id' => $id, 'user_id' => $uid))->find();
        
        //推荐信息
        $roundDZ    = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('feed', $feed);
        $this->assign('mook', $mook);
        $this->assign('list', $list);
        $this->display();
    }

    public function feed() {
        $id = (int) $_POST['mook_id'];
        $uid = session('uid');
        $feedMod = M('MookFeed');
        $feed = $feedMod->where(array('mook_id' => $id, 'user_id' => $uid))->find();
        if($_POST['free_feed_status'] == 1){
            if(!$_POST['send_address']){
                $this->error('开启订阅，未提交地址');
            }else if(!$_POST['send_way']){
                $this->error('开启订阅，未提交发货方式');
            }else if(!$_POST['send_date']){
                $this->error('开启订阅，未提交配送时间');
            }else if(!$_POST['send_condition']){
                $this->error('开启订阅，未提交配送条件');
            }
        }else {
            if(!$_POST['stop_reson']){
                $this->error('停止订阅，未提交原因');
            }
        }
        if(!(int)$_POST['mook_id'])
            $this->error('请选择合适的杂志');
        $_POST['user_id'] = $uid;
        if ($feed) {
            $res = $feedMod->save($_POST);
        } else {
            $res = $feedMod->add($_POST);
        }
        if ($res >= 0) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }

}
